<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>在文件指针中定位</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.fscanf.html">fscanf</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.fstat.html">fstat</a></div>
 <div class="up"><a href="ref.filesystem.html">Filesystem 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.fseek" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fseek</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">fseek</span> &mdash; <span class="dc-title">在文件指针中定位</span></p>

 </div>
 
  <div class="refsect1 description" id="refsect1-function.fseek-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>fseek</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$whence</code><span class="initializer"> = SEEK_SET</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   在与
   <em><code class="parameter">handle</code></em>
   关联的文件中设定文件指针位置。
   新位置从文件头开始以字节数度量，是以
   <em><code class="parameter">whence</code></em>
   指定的位置加上
   <em><code class="parameter">offset</code></em>。
  </p>
  <p class="para">
   In general, it is allowed to seek past the end-of-file; if data is then
   written, reads in any unwritten region between the end-of-file and the
   sought position will yield bytes with value 0. However, certain streams
   may not support this behavior, especially when they have an underlying
   fixed size storage.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fseek-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">handle</code></em></dt>

     <dd>

      <p class="para">文件系统指针，是典型地由
<span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> 创建的 <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>(资源)。</p>
     </dd>

    
    
     <dt>
<em><code class="parameter">offset</code></em></dt>

     <dd>

      <p class="para">
       偏移量。
      </p>
      <p class="para">
       要移动到文件尾之前的位置，需要给
    <em><code class="parameter">offset</code></em> 传递一个负值，并设置 <em><code class="parameter">whence</code></em>
       为 <strong><code>SEEK_END</code></strong>。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">whence</code></em></dt>

     <dd>

      <p class="para">
       <em><code class="parameter">whence</code></em> values are:
       <ul class="simplelist">
        <li class="member"><strong><code>SEEK_SET</code></strong> - 设定位置等于 <em><code class="parameter">offset</code></em> 字节。</li>
        <li class="member"><strong><code>SEEK_CUR</code></strong> - 设定位置为当前位置加上 <em><code class="parameter">offset</code></em>。</li>
        <li class="member"><strong><code>SEEK_END</code></strong> - 设定位置为文件尾加上
    <em><code class="parameter">offset</code></em>。</li>
       </ul>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


  <div class="refsect1 returnvalues" id="refsect1-function.fseek-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    成功则返回 0；否则返回 -1。注意移动到 EOF 之后的位置不算错误。
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.fseek-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2328">
    <p><strong>Example #1 <span class="function"><strong>fseek()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'somefile.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;read&nbsp;some&nbsp;data<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4096</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;move&nbsp;back&nbsp;to&nbsp;the&nbsp;beginning&nbsp;of&nbsp;the&nbsp;file<br />//&nbsp;same&nbsp;as&nbsp;rewind($fp);<br /></span><span style="color: #0000BB">fseek</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.fseek-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    如果使用附加模试（<em>a</em> 或 <em>a+</em>），任何写入文件数据都会被附加上去，而文件的位置将会被忽略，调用 <span class="function"><strong>fseek()</strong></span> 的结果尚未定义。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Not all streams support seeking. For those that do not support seeking,
    forward seeking from the current position is accomplished by reading
    and discarding data; other forms of seeking will fail.
   </p>
  </p></blockquote>
 </div>

 
  <div class="refsect1 seealso" id="refsect1-function.fseek-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.ftell.html" class="function" rel="rdfs-seeAlso">ftell()</a> - 返回文件指针读/写的位置</span></li>
    <li class="member"><span class="function"><a href="function.rewind.html" class="function" rel="rdfs-seeAlso">rewind()</a> - 倒回文件指针的位置</span></li>
   </ul>
  </p>
 </div>

 
</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.fscanf.html">fscanf</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.fstat.html">fstat</a></div>
 <div class="up"><a href="ref.filesystem.html">Filesystem 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
